package com.acxiom.bsh.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.acxiom.bsh.taobao.sdk.TbAuthorize;

@Controller
@RequestMapping("/tb/authorize")
public class TaobaoController {
	@Autowired
	private TbAuthorize tbAuthorize;

	@Value("${ewsDomain}")
	private String ewsDomain;

	@Value("${ewsAppKey}")
	private String ewsAppKey;

	@Value("${ewsAppSecret}")
	private String ewsAppSecret;

	@Value("${ewsSessionKey}")
	private String topSessionKey;

	@Value("${topServiceUrl}")
	private String topServiceUrl;

	@RequestMapping("/profile/{storeId}")
	public String index(ModelMap model, HttpSession session, @PathVariable(value = "storeId", required = false) String storeId, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "error", required = false) String error) {
		String url = ewsDomain;
		try {
			if (StringUtils.isNotBlank(code)) {
				String taobaoNick = tbAuthorize.getTaoBaoId(code);
				session.setAttribute("taobao_user_nick", taobaoNick);
				url = ewsDomain + "/profile/" + storeId;
			} else {
				url += url + "/demo.html";
			}
			if (StringUtils.isNotBlank(error)) {
				return "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:" + url;
	}

	@RequestMapping("/attention/{storeId}")
	public String lottery(ModelMap model, HttpSession session, @PathVariable(value = "storeId", required = false) String storeId, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "error", required = false) String error) {
		String url = ewsDomain;
		if (StringUtils.isNotBlank(code)) {

			String taobaoNick = tbAuthorize.getTaoBaoId(code);
			session.setAttribute("taobao_user_nick", taobaoNick);
			url = ewsDomain + "/attention/" + storeId;
		}
		if (StringUtils.isNotBlank(error)) {
			return "error";
		}
		return "redirect:" + url;
	}

	@RequestMapping("/checkin/{storeId}")
	public String sign(ModelMap model, HttpSession session, @PathVariable(value = "storeId", required = false) String storeId, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "error", required = false) String error) {
		String url = ewsDomain;
		if (StringUtils.isNotBlank(code)) {
			String taobaoNick = tbAuthorize.getTaoBaoId(code);
			session.setAttribute("taobao_user_nick", taobaoNick);
			url = ewsDomain + "/checkin/" + storeId;
		}
		if (StringUtils.isNotBlank(error)) {
			return "error";
		}
		return "redirect:" + url;
	}

	@RequestMapping("/collection/{storeId}")
	public String favorite(ModelMap model, HttpSession session, @PathVariable(value = "storeId", required = false) String storeId, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "error", required = false) String error) {
		String url = ewsDomain;
		if (StringUtils.isNotBlank(code)) {

			String taobaoNick = tbAuthorize.getTaoBaoId(code);
			session.setAttribute("taobao_user_nick", taobaoNick);
			url = ewsDomain + "/collection/" + storeId;
		}
		if (StringUtils.isNotBlank(error)) {
			return "error";
		}
		return "redirect:" + url;
	}

	@RequestMapping("/prize/{storeId}")
	public String prize(ModelMap model, HttpSession session, @PathVariable(value = "storeId", required = false) String storeId, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "error", required = false) String error) {
		String url = ewsDomain;
		if (StringUtils.isNotBlank(code)) {

			String taobaoNick = tbAuthorize.getTaoBaoId(code);
			session.setAttribute("taobao_user_nick", taobaoNick);
			url = ewsDomain + "/prize/" + storeId;
		}
		if (StringUtils.isNotBlank(error)) {
			return "error";
		}
		return "redirect:" + url;
	}

	@RequestMapping("/repair/{storeId}")
	public String repair(ModelMap model, HttpSession session, @PathVariable(value = "storeId", required = false) String storeId, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "error", required = false) String error) {
		String url = ewsDomain;
		if (StringUtils.isNotBlank(code)) {

			String taobaoNick = tbAuthorize.getTaoBaoId(code);
			session.setAttribute("taobao_user_nick", taobaoNick);
			url = ewsDomain + "/repair/" + storeId;
		}
		if (StringUtils.isNotBlank(error)) {
			return "error";
		}
		return "redirect:" + url;
	}

	@RequestMapping("/turntable/{storeId}")
	public String turntable(ModelMap model, HttpSession session, @PathVariable(value = "storeId", required = false) String storeId, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "error", required = false) String error) {
		String url = ewsDomain;
		if (StringUtils.isNotBlank(code)) {

			String taobaoNick = tbAuthorize.getTaoBaoId(code);
			session.setAttribute("taobao_user_nick", taobaoNick);
			url = ewsDomain + "/turntable/" + storeId;
		}
		if (StringUtils.isNotBlank(error)) {
			return "error";
		}
		return "redirect:" + url;
	}

}
